Data source: Centadata by HK Centaline Agency Limited.
Centa-City Leading Sub-index by Regions:
HK (Hong Kong Island): CW, EA, SO, WC
KL (Kowloon): SS, KC, KU, WT, YT
NTE (New Territories East): ST, TP, SK, NO
NTW (New Territories West): TW, KI, TM, YL, IS
Home Prices for Selected Estates: In each of 4 regions, we pick 3 estates and download their historical weekly prices. URL: http://202.72.14.52/p2/cci/SearchHistory.aspx
library(plotly)
library(shiny)
library(xts)
DataX = read.csv("HKHomeCCLI.csv")
TsX = xts(DataX[,-1], order.by=as.Date(DataX[,1], "%Y/%m/%d"))
library(dygraphs)
dygraph(TsX, main = "HK Home Price: Centa-City Leading Index by Regions") %>%
dyRangeSelector(dateWindow = c("1994-01-01", "2020-03-31")) %>%
dyOptions(colors = c("red", "green", "blue", "cyan"))DataX %>% plot_ly(x = ~Date, y = ~HK, name="HK Island", type = "scatter", mode="lines") %>%
add_trace(y = ~KL, name="Kowloon", type = "scatter") %>%
add_trace(y = ~NTE, name="New Territoryies (East)", type = "scatter") %>%
add_trace(y = ~NTW, name="New Territoryies (West)", type = "scatter") %>%
layout(legend = list(x = 0.02, y = 0.98),
title = "HK Centa-City Leading Index by Regions")Load Map Visualization with Region Defintion
library(ggplot2); library(sp)
hkmapraw = readRDS("HKG_adm1.rds")
meta = data.frame(id=hkmapraw$ID_1, Code=hkmapraw$HASC_1)
meta$Region = "NTW"
meta$Region[meta$Code %in% c("HK.ST", "HK.TP", "HK.SK", "HK.NO")] = "NTE"
meta$Region[meta$Code %in% c("HK.CW", "HK.EA", "HK.SO", "HK.WC")] = "HK"
meta$Region[meta$Code %in% c("HK.SS", "HK.KC", "HK.KU", "HK.WT", "HK.YT")] = "KL"
meta$Region=as.factor(meta$Region)
hkmap = merge(fortify(hkmapraw), meta, by="id")
hkmap$rowid = seq(1, dim(hkmap)[1])
summary(hkmap)## id long lat order hole piece group
## Length:25509 Min. :113.8 Min. :22.15 Min. : 1 Mode :logical 1 :15768 8.1 : 2968
## Class :character 1st Qu.:114.0 1st Qu.:22.27 1st Qu.: 430 FALSE:25509 2 : 4391 3.1 : 2371
## Mode :character Median :114.2 Median :22.35 Median :1121 3 : 1237 7.1 : 1574
## Mean :114.2 Mean :22.35 Mean :1639 5 : 803 12.2 : 1383
## 3rd Qu.:114.3 3rd Qu.:22.43 3rd Qu.:2569 4 : 729 12.1 : 1357
## Max. :114.4 Max. :22.56 Max. :5719 6 : 421 11.1 : 1302
## (Other): 2160 (Other):14554
## Code Region rowid
## HK.IS :5719 HK : 2490 Min. : 1
## HK.SK :5021 KL : 1249 1st Qu.: 6378
## HK.TP :3344 NTE:11517 Median :12755
## HK.NO :2263 NTW:10253 Mean :12755
## HK.SO :1597 3rd Qu.:19132
## HK.TM :1298 Max. :25509
## (Other):6267
Customize the map region colors by home prices:
library(magick)
img <- image_graph(800, 600, res = 72)
# iidx = c(seq(dim(DataX)[1], 1, -104), 1)
iidx = seq(70, 1, -4)
for (i in iidx){
TmpX = data.frame(Region=names(DataX)[-1], Price=as.numeric(DataX[i,-1]))
MapDataX = merge(hkmap, TmpX, by="Region", sort=FALSE)
p = ggplot(MapDataX[order(MapDataX$rowid),], aes(long, lat, group=group, fill=Price)) +
geom_polygon() +
scale_fill_gradient(limits=c(150,205), low = "green", high = "red") +
ggtitle(paste("Home Price Index by Regions as of", DataX$Date[i])) +
theme(plot.title = element_text(size = 20, face = "bold"))
print(p)
}
dummy = dev.off()
img %>% image_trim() %>% image_animate(fps = 1, loop=1) %>% image_write("HomePriceMap.gif")Using the data of home prices for 12 selected estates
DataX = read.csv("HKHomePrice.csv")
DataX$Date = as.POSIXlt(DataX$Date, format="%d/%m/%Y") # handle date/time in R
DataX = DataX[order(DataX$Date), ]
DataX = DataX[DataX$Date >= "2014-01-01", ]
head(DataX)## Date Marina.Square Bel.Air Kornhill Ocean.Shores Arch Parc.Oasis Ocean.View City.One Festival.City
## 325 2014-01-05 10901.9 19474.0 11929.4 10843.0 28775.6 14563.6 9796.88 10112.1 11164.5
## 324 2014-01-12 11217.9 19494.0 11874.6 10761.1 28536.2 14453.6 9766.75 10081.0 11309.4
## 323 2014-01-19 11619.4 19561.6 11860.5 10737.8 28551.6 14422.4 9788.78 10155.4 10656.5
## 322 2014-01-26 11780.0 19565.2 11692.9 11002.1 28467.8 14531.6 9803.13 10242.2 11534.5
## 321 2014-02-02 11727.2 19554.2 11731.0 10653.2 28693.8 14387.8 9827.07 10110.2 10825.2
## 320 2014-02-09 12229.9 19575.5 11640.5 10776.8 28679.1 14387.4 9820.91 10103.9 10742.7
## Yoho.Town Gold.Coast D.Park
## 325 9381.04 7241.19 8774.89
## 324 9488.51 7250.07 8785.64
## 323 9623.94 7319.71 8870.04
## 322 9712.53 7302.66 9025.65
## 321 9522.17 7305.13 9028.71
## 320 9386.51 7318.47 8835.96
n = dim(DataX)[1]
m = dim(DataX)[2]-1
matplot(DataX[, -1], type="l", lty=1, col=rainbow(m), xaxt="n", ylab="", xlim=c(1,n+10))
axis(1,at=1:n,labels=format(DataX$Date, "%Y%m"))
text(n, tail(DataX[,-1], 1), names(DataX)[-1], p=4, cex=0.6)
title(main="Home Prices for Selected Estates in Hong Kong",
xlab="Time", ylab="Unit Price (net area)")Space-Time Heatmap:
tmp = as.matrix(DataX[,-1])
par(mar=c(1,1,1,1)); image(tmp, col=terrain.colors(m), axes=FALSE)
text(0, seq(0,1,length.out=m), names(DataX)[-1], p=4, cex=1)